﻿using Anke.SHManage.BLL;
using Anke.SHManage.MSSQLDAL;
using Anke.SHManage.MSSQLDAL.TJDAL;
using Anke.SHManage.Utility;
using Microsoft.Reporting.WebForms;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Anke.SHManage.Web.Reports
{
    public partial class CarManage_YearMonthModeWebForm : System.Web.UI.Page
    {
        private string reportName;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                InitPage();
            }
            else
            {
                reportName = Convert.ToString(ViewState["reportName"]);
            }
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindReportDataSource();

        }
        private void InitPage()
        {
            reportName = Request.QueryString["ReportName"];
            ViewState["reportName"] = reportName;

            switch (reportName)
            {
                case "CM_AnnualSurvey.rdlc":
                    this.StartDate.Text = DateTime.Now.ToString("yyyy-MM-01 00:00:00");
                    this.EndDate.Text = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
                    break;
                case "CM_Insurance.rdlc":
                    this.StartDate.Text = DateTime.Now.ToString("yyyy-MM-01 00:00:00");
                    this.EndDate.Text = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
                    break;

                case "CM_Accident.rdlc":
                    this.StartDate.Text = DateTime.Now.ToString("yyyy-01-01 00:00:00");
                    this.EndDate.Text = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
                    break;
                case "DriverReportUnPickList.rdlc":
                    this.StartDate.Text = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd 00:00:00");
                    this.EndDate.Text = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
                    break;
                default:
                    break;
            }



            BindReportDataSource();
        }
        protected void BindReportDataSource()
        {

            DateTime dateStart = Convert.ToDateTime(this.StartDate.Text);
            DateTime dateEnd = Convert.ToDateTime(this.EndDate.Text);
            string stationID = Convert.ToString(this.Station.Text);
            string centerID = Convert.ToString(this.Center.Text);
            string zPlateNumber = Convert.ToString(this.zPlateNumber.Text);
            string start1 = dateStart.ToString();
            string end1 = dateEnd.ToString();
            string eventDispatchCenterID = UserOperateContext.Current.Seesion_Dispatch_SubCenterID; //2020-11-22 zch 登录人所处急救中心



            this.ReportViewer1.LocalReport.DataSources.Clear();
            this.ReportViewer1.LocalReport.ReportPath = Server.MapPath(@"~/Reports/" + reportName);
            Microsoft.Reporting.WebForms.ReportParameter st = new Microsoft.Reporting.WebForms.ReportParameter("StartTime", start1);
            Microsoft.Reporting.WebForms.ReportParameter ed = new Microsoft.Reporting.WebForms.ReportParameter("EndTime", end1);
            this.ReportViewer1.LocalReport.SetParameters(new Microsoft.Reporting.WebForms.ReportParameter[] { st, ed });

            CMTJDAL dal = new CMTJDAL();
            TJDAL tdal = new TJDAL();
            switch (reportName)
            {
                case "CM_AnnualSurvey.rdlc":
                    this.Page.Title = "车辆年检表";
                    DataTable dt = dal.GetLSNianJian(dateStart, dateEnd, centerID, stationID, zPlateNumber);
                    this.ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSetForCMAS", dt));
                    break;
                case "CM_Insurance.rdlc":
                    this.Page.Title = "车辆保险表";
                    DataTable dt1 = dal.GetLSBaoXian(dateStart, dateEnd, centerID, stationID, zPlateNumber);
                    this.ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSetForCMI", dt1));
                    break;
                case "CM_Accident.rdlc":
                    this.Page.Title = "车辆事故表";
                    DataTable dt2 = dal.GetLSShiGu(dateStart, dateEnd, centerID, stationID, zPlateNumber);
                    this.ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSetForCMAC", dt2));
                    break;
                case "DriverReportUnPickList.rdlc":

                    E_StatisticsPermisson em = UserOperateContext.Current.getMaxPerForStatistics();
                    string selfCenterID = UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSubCenterID != null ? UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSubCenterID : "";
                    string selfEMCenterID;
                    if (selfCenterID.Length > 0)
                    {
                        selfEMCenterID = selfCenterID;
                    }
                    else
                    {
                        selfEMCenterID = "-1";
                    }
                    string selfStationID = UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSationID != null ? UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSationID : "";
                    string selfEMStationID;
                    if (selfStationID.Length > 0)
                    {
                        selfEMStationID = selfStationID;
                    }
                    else
                    {
                        selfEMStationID = "-1";
                    }
                    string selfWorkID = UserOperateContext.Current.Session_UsrInfo.WorkCode != null ? UserOperateContext.Current.Session_UsrInfo.WorkCode : "";
                    string selfEMWorkID;
                    if (selfWorkID.Length > 0)
                    {
                        selfEMWorkID = selfWorkID; 
                    }
                    else
                    {
                        selfEMWorkID = "-1";
                    }
                    this.Page.Title = "日报表未勾选任务统计表";
                    DataTable dt3 = tdal.GetUnPickDriverReport(dateStart, dateEnd, centerID, stationID, zPlateNumber, em, selfEMCenterID, selfEMStationID, selfEMWorkID, eventDispatchCenterID);
                    this.ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", dt3));
                    break;

                default:
                    break;
            }
            this.ReportViewer1.LocalReport.Refresh();
        }

    }
}